#include "usart.h"
extern uchar com_ok;
xdata uchar comReadData[];
uchar Read_con;

extern void delay_ms(uint ms);
void data_clear()
{
	uchar m;
	while(comReadData[m])
	{
		comReadData[m]=0;
		m++;
	}
	Read_con=0;
}


void command_math()
{
	Read_con=0;	
	if (com_ok==0)
	{
		uchar str[]="read?";
		uchar m=0;		
		while(str[m])
		{
			if (str[m]==comReadData[m])
			{
				m++;
			}
			else
			{
				goto AB;
			}
		}
		com_ok=2;
	}
	else if(com_ok==1)
	{
		uchar str[]="Gooo";
		uchar m;	
		//
		while(str[m])
		{
			if (str[m]==comReadData[m])
			{
				m++;
			}
			else
			{
				goto AB;
			}
		}
		com_ok=2; 
	}
AB:	data_clear();
}

void WriteChar(uchar date)
{
	comReadData[Read_con]=date;
	Read_con++;
}

void SendData(uchar dat)
{
	while (!TI);                    //Wait for the previous data is sent
	TI = 0;                         //Clear TI flag
	SBUF = dat;                     //Send current data
}

void SendStr(uchar *str)
{
	//ES=0;
	REN=0;
	while (*str)
	{
		SendData(*str++);
	}
	SendData('\n');
	SendData('\r');
	
	REN=1;
	//ES=1;
}

void InitUart()    
{
    SCON = 0x5a;                    //8 bit data ,no parity bit
	PCON &= 0x7F;                   
    TMOD = 0x20;                    //T1 as 8-bit auto reload
    TH1 = TL1 = 0xfd;//-(FOSC/12/32/BAUD); //Set Uart baudrate
    TR1 = 1;                        //T1 start running
	ES=1;
}